using System;
using System.Collections.Generic;
using System.Text;
using GraphAlgsPresentation.Core;
using System.ComponentModel;
using System.Xml.Serialization;

namespace GraphAlgsPresentation.ScriptLibrary
{
    /// <summary>
    /// Provides a change edge label command
    /// </summary>
    [Serializable]
    public class ChangeEdgeLabel:Command
    {
        private string srcName, dstName;
        private string newLabel;
        private String oldLabel;

        #region Properties
        /// <summary>
        /// Gets or sets the old label.
        /// </summary>
        /// <value>The old label.</value>
        [Browsable(false)]
        [XmlIgnore]
        public String OldLabel
        {
            get { return oldLabel; }
            set { oldLabel = value; }
        }
	
        /// <summary>
        /// returns source vertex's name
        /// </summary>
        public string SourceName
        {
            get { return srcName; }
            set { srcName = value; }
        }

        /// <summary>
        /// returns destination vertex's name
        /// </summary>
        public string DestinationName
        {
            get { return dstName; }
            set { dstName = value; }
        }

        /// <summary>
        /// return new edge's label
        /// </summary>
        public string NewLabel
        {
            get { return newLabel; }
            set { newLabel = value; }
        }
        #endregion


        /// <summary>
        /// Initializes a new instance of the <see cref="ChangeEdgeLabel"/> class.
        /// </summary>
        /// <param name="v1">Source vertex's name</param>
        /// <param name="v2">Destination vertex's name</param>
        /// <param name="NewLabel">New edge's label</param>
        /// <param name="effect">A DrawEffect enum. Default is DrawEffect.NoEffect</param>
        public ChangeEdgeLabel(string v1, string v2, string NewLabel, DrawEffect effect)
        {
            srcName = v1;
            dstName = v2;
            newLabel = NewLabel;
            Effect = effect;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ChangeEdgeLabel"/> class.
        /// </summary>
        /// <param name="v1">Source vertex's id</param>
        /// <param name="v2">Destination vertex's id</param>
        /// <param name="NewLabel">New edge's label</param>
        /// <param name="effect">A DrawEffect enum. Default is DrawEffect.NoEffect</param>
        public ChangeEdgeLabel(int v1, int v2, string NewLabel, DrawEffect effect)
        {
            this.srcName = (v1 + 1).ToString();
            this.dstName = (v2 + 1).ToString();
            this.newLabel = NewLabel;
            this.Effect = effect;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ChangeEdgeLabel"/> class.
        /// </summary>
        public ChangeEdgeLabel()
        {
        }

        /// <summary>
        /// Runs this instance.
        /// </summary>
        public override void Run()
        {
            LCEdge e = this.Context.GetEdge(srcName, dstName);
            oldLabel = e.Label;
            e.Label = newLabel;
            e.Updated = true;
            //this.Context.Layout.ReDraw();
        }

        /// <summary>
        /// Undoes this instance.
        /// </summary>
        public override void Undo()
        {
            LCEdge e = this.Context.GetEdge(srcName, dstName);
            e.Label = OldLabel;
            e.Updated = true;
        }
    }
}
